package com.example.joke.custom.tbgrade

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.example.joke.R

class RatingBar : View {

    private var mGradeNum = 5
    private var mNormalBitmap: Bitmap
    private var mFocusBitmap: Bitmap
    private var mPaint: Paint
    private var mCurrentGradeNum = 0

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        val typedArray = context?.obtainStyledAttributes(attrs, R.styleable.RatingBar)
        val normalId = typedArray?.getResourceId(R.styleable.RatingBar_starNormal, 0)
        val focusId = typedArray?.getResourceId(R.styleable.RatingBar_starFocus, 0)
        mGradeNum = typedArray?.getInt(R.styleable.RatingBar_gradeNum, mGradeNum)!!
        typedArray?.recycle()

        if (normalId == 0 || focusId == 0) {
            throw Exception("请设置属性starNormal和starFocus")
        }

        mNormalBitmap = BitmapFactory.decodeResource(resources, normalId!!)
        mFocusBitmap = BitmapFactory.decodeResource(resources, focusId!!)

        mPaint = Paint()
        mPaint.isAntiAlias = true
        mPaint.isDither = true
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val height = mNormalBitmap.height
        val width = mNormalBitmap.width * mGradeNum
        setMeasuredDimension(width, height)
    }

    override fun onDraw(canvas: Canvas?) {
        // i * mFocusBitmap.width, i * 星星的宽度
        for (i in 0 until mGradeNum) {
            // 触摸的时候，mCurrentGradeNum值是不断变化的
            if (mCurrentGradeNum > i) { // mCurrentGradeNum: 1,2...,     i: 0,1,2...
                // 当前分数之前的星星都是点亮的
                canvas?.drawBitmap(mFocusBitmap, i * mFocusBitmap.width.toFloat(), 0F, null)
            } else {
                canvas?.drawBitmap(mNormalBitmap, i * mFocusBitmap.width.toFloat(), 0F, null)
            }
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event?.action) {
            // 按下，移动，抬起，处理逻辑都是一样的，尽量减少onDraw调用，所以只处理move
//            MotionEvent.ACTION_DOWN -> {}
//            MotionEvent.ACTION_UP -> {}
            MotionEvent.ACTION_MOVE -> {
                // 根据当前位置计算出分数，再去刷新显示
                // event.x：相对于当前控件的位置，event.rawX：相对于屏幕的位置，获取屏幕的x位置
                val x = event.x
                var current = (x / mFocusBitmap.width).toInt() + 1
                // 范围问题处理
                if (current < 0) {
                    current = 0
                }
                if (current > mGradeNum) {
                    current = mGradeNum
                }
                // 尽量减少onDraw的调用，分数相同的情况下，就不要绘制了
                if (current == mCurrentGradeNum) {
                    return true
                }
                mCurrentGradeNum = current
                invalidate()
            }
            else -> {}
        }
        // 返回false，不消费，第一次down返回false后，down以后的事件就进不来了
        return true
    }
}